<!--
 * @Author: 郭安东
 * @Date: 2021-03-12 16:51:04
 * @LastEditTime: 2021-04-11 19:15:42
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \test\src\views\function\temperature.vue
-->
<template>
  <div class="temperature">
    <el-card class="box-card">
      <el-link class="userName">{{ userName }}</el-link>
      <el-tag :type="statesType" style="margin-left: 30px">{{ states }}</el-tag>
      <span>
        <el-button class="loginOut-btn" @click="dialog = true"><i class="el-icon-switch-button"></i></el-button>
      </span>
    </el-card>
    <el-dialog
      title="确定注销用户吗"
      :visible.sync="dialog"
      width="30%"
      center>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="loginOut()">确 定</el-button>
        <el-button @click="dialog = false">取 消</el-button>
      </span>
    </el-dialog>
    <four-menu :buts-data="dataFourMenu" :title=" '用户管理' " class="four-menu"/>
    <el-card class="box-card">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="按名字查询：" v-show="role === 'role_admin'">
          <el-input placeholder="请输入内容" v-model="searchForm.userName" class="searchInput"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" round @click="searchUser()">搜索</el-button>
          <el-button round @click="getUserInfomation()">重置</el-button>
        </el-form-item>
      </el-form>
      <common-table
        ref="mainTableRef"
        :table-data="mainTableData"
        :table-label="mainTableLabel"
        :table-config="mainTableConfig"
        :default-sort="mainTableConfig.defaultSort"
        style="height: 480px; margin-top: 10px"
        @handle-size-change="handleSizeChange"
        @handle-current-change="handleCurrentChange">
        <template slot="operating" slot-scope="scope">
          <el-button class="operatingButton" @click="deleteUserfct(scope.row)">
            删除
          </el-button>
          <el-button class="operatingButton" @click="getBasicInfo(scope.row)">
            查看
          </el-button>
        </template>
      </common-table>
    </el-card>
    <el-dialog
      title="用户信息"
      :visible.sync="centerDialogVisible"
      width="30%"
      center>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="form.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="社区">
          <el-input v-model="form.community" disabled></el-input>
        </el-form-item>
        <el-form-item label="小区">
          <el-input v-model="form.estate" disabled></el-input>
        </el-form-item>
        <el-form-item label="房号">
          <el-input v-model="form.houseNumber" disabled></el-input>
        </el-form-item>
        <el-form-item label="身份证">
          <el-input v-model="form.idCard" disabled></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-input v-model="form.gender" disabled></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="form.age" disabled></el-input>
        </el-form-item>
        <el-form-item label="电话号码">
          <el-input v-model="form.phone" disabled></el-input>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import commonTable from '../../components/common-table/index'
import FourMenu from '../../components/menu/four-menu'
import { getUserInfo, deleteUser, userLoginOut, getUserBasicInfo } from '../../api/apply-info'
export default {
  name: 'User',
  components: { commonTable, FourMenu },
  data () {
    return {
      userName: '',
      role: '',
      states: '',
      statesType: '',
      searchData: {
        userName: '',
        pageSize: 10,
        currentPage: 1
      },
      centerDialogVisible: false,
      dialog: false,
      form: {
        name: '',
        community: '',
        estate: '',
        houseNumber: '',
        idCard: '',
        gender: '',
        age: '',
        phone: ''
      },
      searchForm: {
        userName: '',
        pageSize: 10,
        currentPage: 1
      },
      // 删除用户传参
      deleteData: {
        id: ''
      },
      index: 0,
      dataFourMenu: [{ name: '返回' }],
      mainTableData: [
        { id: 'id', userName: '用户名' }
      ],
      mainTableLabel: [
        {
          prop: 'id',
          label: '编号',
          width: 125,
          sortable: true
        },
        {
          prop: 'userName',
          label: '用户名',
          width: 125,
          sortable: true
        },
        {
          prop: 'operating',
          label: '操作',
          fixed: 'right',
          width: 200,
          slot: true
        }
      ],
      mainTableConfig: {
        // 是否显示loading效果
        loading: false,
        // 是否开启分页
        pagination: true,
        // 当前页数
        page: 1,
        // 当前页条数
        count: 10,
        // 数据总条数
        totalCount: 0,
        // 每页显示条数
        pageSize: 10,
        // 每页显示条数选择项
        pageSizeOptions: [10, 20, 30, 40],
        // 是否开启隔行换色
        stripe: true,
        // 是否生成序号列
        orderNum: false,
        // 是否生成行选择框列
        rowSelect: true,
        // 表格唯一ID字段
        uniqueId: 'eventNo',
        // 表格高度，低于所有行高时会生成滚动条，高于所有数据的高度会留白
        tableHeight: '90%',
        // 填入表头高度和字体大小的样式对象
        headerSize: {
          height: '40px',
          'font-size': '15px'
        },
        // 填入表头背景色和字体色的样式对象
        headerColor: {
          color: '#333333',
          'background-color': '#e4eefc'
        },
        // 填入表格高度和字体大小的样式对象
        rowSize: {
          height: '35px',
          'font-size': '14px'
        },
        // 填入表格背景色和字体色的样式对象
        rowColor: {
          color: '#333333',
          'background-color': '#f7faff'
        },
        // 是否开启点击选中行
        clickRow: false,
        showTooltip: true
      }
    }
  },
  mounted () {
    this.getUserName()
    this.getUserInfomation()
  },
  methods: {
    // 获取账户id
    getUserName () {
      this.userName = sessionStorage.getItem('userName')
      this.role = sessionStorage.getItem('role')
      this.states = sessionStorage.getItem('states')
      if (this.states === '状态正常' || this.states === '管理员') {
        this.statesType = 'success'
      } else {
        this.statesType = 'danger'
      }
      console.log('role', this.role)
    },
    // 注销用户
    loginOut () {
      userLoginOut().then(res => {
        console.log('loginOut', res)
        this.dialog = false
        this.$message(res.data.message)
        // 将用户名放入sessionStorage
        sessionStorage.setItem('userName', null)
        sessionStorage.setItem('userId', null)
        sessionStorage.setItem('role', null)
        // 将用户放入vuex
        this.$store.dispatch('setUser', null)
        this.$router.push({ path: '/login' })
      })
    },
    // 每页条数发生改变时触发
    handleSizeChange (val) {
      this.mainTableConfig.page = 1
      this.mainTableConfig.pageSize = val
      this.mainTableData = []
      // this.getFaultTripTableData()
    },
    // 当前页发生改变时触发
    handleCurrentChange (val) {
      console.log(val)
      this.mainTableConfig.page = val
      this.searchData.currentPage = val
      this.getUserInfomation()
    },
    onSubmit () {
      console.log('保存成功')
    },
    // 获取用户信息
    getUserInfomation () {
      getUserInfo(this.searchData).then(res => {
        console.log('this is res', res)
        this.mainTableData = res.data.data.records
        this.mainTableConfig.totalCount = res.data.data.total
      })
    },
    // 获取用户基础信息
    getBasicInfo (row) {
      console.log(row)
      if (row.userName !== 'admin') {
        getUserBasicInfo(row.id).then(res => {
          console.log(res)
          if (res.data.data !== '') {
            this.centerDialogVisible = true
            this.form = {}
            this.form = res.data.data
          }
        })
      } else {
        this.$message('管理员无基础信息')
      }
    },
    // 删除用户
    deleteUserfct (row) {
      console.log(row)
      this.deleteData.id = row.id
      deleteUser(this.deleteData.id).then(res => {
        console.log('delete res', res)
        this.$message(res.data.message)
        this.getUserInfomation()
      })
    },
    // 按条件查询
    searchUser () {
      getUserInfo(this.searchForm).then(res => {
        console.log('this is res', res.data.data.records)
        this.mainTableData = res.data.data.records
      })
    }
  }
}
</script>
<style scoped>
.box-card {
    margin-top: 10px;
    margin-left: 10%;
    width: 1200px;
    border-radius: 20px;
    background-color: rgba(255, 255, 255, 0.8);
}
.userName{
  color: #7196b1;
  font-size: 16px;
  font-weight: bolder;
}
.loginOut-btn{
  float: right;
  margin-top: -5px;
  border-radius: 10px;
  color: #7196b1;
  border: none;
  background-color: rgb(240, 234, 234, 0.6);
}
.four-menu {
  margin-top: 10px;
  margin-bottom: 10px;
}
.form-btn {
  margin-left: 15%;
}
</style>
<style>
body{
  background-image: url('../../assets/bg3.jpeg');
  background-size: 100% 1000px;
  background-repeat: no-repeat;
}
.el-icon-switch-button {
  color: #e4252d;
  font-size: 17px;
  font-weight: bolder;
}
</style>
